#include "BinAnimationReader.h"

BinAnimationReader::BinAnimationReader(tstring filename):BinaryReader(filename),
	m_Header(0)
{
}

BinAnimationReader::~BinAnimationReader(void)
{
}

bool BinAnimationReader::Read()
{
	if ( Open() ){
		m_Header = this->ReadLittleInt();
		tstring name = this->ReadAsciiString();
		int startFrame = this->ReadLittleInt();
		int stopFrame = this->ReadLittleInt();
		m_pAnimation = new Animation(name,startFrame,stopFrame);
		int nrOfBones = this->ReadLittleShort();
		for (int i =0; i< nrOfBones;++i){
			tstring boneName = this->ReadAsciiString();
			m_pStoredAnimationNode = new StoredAnimationNode(boneName);
			m_pAnimation->AddAnimationNode(m_pStoredAnimationNode);

			for (int i = startFrame ; i <= stopFrame; ++i){
				short t = this->ReadLittleShort();
				float rx = this->ReadLittleFloat();
				float ry = this->ReadLittleFloat();
				float rz = this->ReadLittleFloat();
				float tx = this->ReadLittleFloat();
				float ty = this->ReadLittleFloat();
				float tz = this->ReadLittleFloat();
				float sx = this->ReadLittleFloat();
				float sy = this->ReadLittleFloat();
				float sz = this->ReadLittleFloat();
				m_pStoredAnimationNode->AddTransformAt(t,DAEFloat4(rx,ry,rz,1),DAEFloat3(tx,ty,tz),DAEFloat3(sx,sy,sz));
			}
		}

		this->Close();
		return true;
	}else{
		return false;
	}
}
